package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entify.Setmeal;
import com.itheima.reggie.entify.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl
        extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private  SetmealDishService setmealDishService;
    //新增套餐
    @Override
    @Transactional
    public R saveSetmeal(SetmealDto setmealDto) {

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //判断菜品是否符合要求
        if(CollUtil.isEmpty(setmealDishes)){
            return R.error("套餐中菜品数据为空，请务必选择菜品");
        }
        //以上的数据套餐保持基本不动，存入setmeal套餐表,检查数据的完整性
        int insert = setmealMapper.insert(setmealDto);
        //套餐下的菜品，存入setmeal_dish套餐菜品表中
        Long id = setmealDto.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            setmealDishMapper.insert(setmealDish);
        }
        return insert>0?R.success("套餐保存成功"):R.error("套餐保存失败");
    }
    //删除套餐
    @Override
    @Transactional
    public boolean deleteWithDish(List<Long> ids) {
        //判断当前删除的套餐是否处于售卖状态
        LambdaUpdateWrapper<Setmeal> uw = new LambdaUpdateWrapper<>();
        uw.in(Setmeal::getId,ids);
        uw.eq(Setmeal::getStatus,1);
        int count = this.count(uw);
        //如果处于售卖状态，则不能删除
        if(count>0){
            return false;
        }
        //如果处于停售状态，先删除套餐表中的数据
        this.removeByIds(ids);

        //删除关系表中的数据
        LambdaUpdateWrapper<SetmealDish> duw = new LambdaUpdateWrapper<>();
        duw.in(SetmealDish::getId,ids);
        setmealDishService.remove(duw);
        return true;
    }
//根据id查询套餐数据
    @Override
    public SetmealDto getByIdSetmeal(Long id) {
        //从setmeal 中获取基本信息
        Setmeal setmeal = this.getById(id);

        //创建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐对应的菜品，从套餐中取出
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();

        qw.eq(SetmealDish::getSetmealId,setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(qw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    //修改套餐
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //保存套餐表中的数据
        this.updateById(setmealDto);

        //先删除对应菜品的数据
        LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(dqw);

        //将新修改的元素插入到setmeal表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
